package com.aide.simplification.popedom.taglib;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.aide.simplification.global.Utils;
import com.aide.simplification.popedom.Publiclass;
import com.aide.simplification.popedom.login.LoginUser;
import com.aide.simplification.popedom.login.PopSSO;
import com.aide.simplification.popedom.popedom.base.PopedomOP;

public class Pop extends TagSupport {

	private static final long serialVersionUID = -5185302047931093878L;

	private String pop = "";// ����Ȩ��
	private String logic = "and";// ����or ȫ��and
	private boolean isBreak = false;// �Ƿ����
	private String forwardurl = "/"; // ���·��
	private String scope = "";// ���÷�Χ up�ϼ� down �¼� noself������ĳ�û�
	private String self = "";// �û����
	private boolean not = false;// �Ƿ�ȡ��
	private boolean isPop = false;// �Ƿ���Ȩ��
	private int level = 99;
	public String getForwardurl() {
		return forwardurl;
	}

	public void setForwardurl(String forwardurl) {
		this.forwardurl = forwardurl;
	}

	public void setPop(String pop) {
		this.pop = pop;
	}

	public String getPop() {
		return pop;
	}

	public int doStartTag() throws JspException {
		ServletRequest request = pageContext.getRequest();
		ServletResponse response = pageContext.getResponse();
		HttpServletRequest httprequest = (HttpServletRequest) request;
		String basepath = "";
		if (request.getAttribute("path") != null) {
			basepath = request.getAttribute("path").toString();
		}
		if (forwardurl.equalsIgnoreCase("")) {
			forwardurl = "/nonepop.jsp";
		}
		if (basepath.length() > 0) {
			forwardurl = basepath + forwardurl;
		}
		LoginUser loginUser = null;
		PopSSO popsso = new PopSSO(request, response);
		loginUser = popsso.ssoSync();
		
		if (loginUser != null && loginUser.getPop() != null) {
			if(level==99){
				level=loginUser.getLevel();
			}
			String upop = "";
			PopedomOP op = new PopedomOP();
			Publiclass pc = new Publiclass();
			if (loginUser.getUseruuid().equals(Utils.getConfig("pop.admin"))) {
				this.isPop = true;
			} else if ("".equals(self)) {
				upop = loginUser.getPop().toString();
				if (this.logic.equalsIgnoreCase("or")) {
					if (op.checkPopOr(pop, upop)) {
						this.isPop = true;
					} else {
						this.isPop = false;
					}
				} else {
					if (op.checkPopAnd(pop, upop)) {
						this.isPop = true;
					} else {
						this.isPop = false;
					}
				}
			} else {
				if (self.equals(loginUser.getUseruuid())) {
					this.isPop = true;
				} else if (scope.equals("up")) {
					String useruuids = pc.getUseruuidsUp(httprequest, response,
							self, pop);
					//System.out.println("up:" + useruuids);
					if (useruuids.indexOf("'" + loginUser.getUseruuid() + "'") != -1) {
						this.isPop = true;
					} else {
						this.isPop = false;
					}
				} else if (scope.equals("down")) {
					String useruuids = pc.getUseruuidsDown(httprequest,
							response, self, pop);
					//System.out.println("down:" + useruuids);
					if (useruuids.indexOf("'" + loginUser.getUseruuid() + "'") != -1) {
						this.isPop = true;
					} else {
						this.isPop = false;
					}
				} else if (scope.equals("noself")) {
					if (self.equals(loginUser.getUseruuid())) {
						this.isPop = false;
					} else {
						String useruuids = pc.getUseruuidsUp(httprequest,
								response, self, pop);
						if (useruuids.indexOf("'" + loginUser.getUseruuid()
								+ "'") != -1) {
							this.isPop = true;
						} else {
							this.isPop = false;
						}
					}
				} else {
					upop = loginUser.getPop().toString();
					if (this.logic.equalsIgnoreCase("or")) {
						if (op.checkPopOr(pop, upop)) {
							this.isPop = true;
						} else {
							this.isPop = false;
						}
					} else {
						if (op.checkPopAnd(pop, upop)) {
							this.isPop = true;
						} else {
							this.isPop = false;
						}
					}
				}
			}
		} else {
			this.isPop = false;
		}
		if (not) {
			this.isPop = !this.isPop;

		}
		if (isPop) {
			return EVAL_BODY_INCLUDE;
		} else {
			return SKIP_BODY;
		}
	}

	public int doEndTag() throws JspException {
		ServletRequest request = pageContext.getRequest();
		ServletResponse response = pageContext.getResponse();

		if (this.isPop || !isBreak) {
			return EVAL_PAGE;
		} else {
			try {
				request.getRequestDispatcher(forwardurl).forward(request,
						response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return SKIP_PAGE;
		}
	}

	public String getLogic() {
		return logic;
	}

	public void setLogic(String logic) {
		this.logic = logic;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getSelf() {
		return self;
	}

	public void setSelf(String self) {
		this.self = self;
	}

	public boolean isIsBreak() {
		return isBreak;
	}

	public void setIsBreak(boolean isBreak) {
		this.isBreak = isBreak;
	}

	public boolean isNot() {
		return not;
	}

	public void setNot(boolean not) {
		this.not = not;
	}
}
